home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / OTFindSerialPorts1.0d1 / OTFindSerialPorts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-28  |  4.2 KB  |  146 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        OTFindSerialPorts.c
  3.  
  4.     Contains:    Sample to show how to find all the serial ports using OT.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.     You may incorporate this sample code into your applications without
  13.     restriction, though the sample code has been provided "AS IS" and the
  14.     responsibility for its operation is 100% yours.  However, what you are
  15.     not permitted to do is to redistribute the source as "DSC Sample Code"
  16.     after having made changes. If you're going to re-distribute the source,
  17.     we require that you make it clear in the source that the code was
  18.     descended from Apple Sample Code, but that you've made changes.
  19. */
  20.  
  21. /////////////////////////////////////////////////////////////////////
  22. // The OT debugging macros in <OTDebug.h> require this variable to
  23. // be set.
  24.  
  25. #ifndef qDebug
  26. #define qDebug    1
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////
  30. // Pick up all the standard OT stuff.
  31.  
  32. #include <OpenTransport.h>
  33.  
  34. /////////////////////////////////////////////////////////////////////
  35. // Pick up device type definitions.
  36.  
  37. #include <OpenTptLinks.h>
  38.  
  39. /////////////////////////////////////////////////////////////////////
  40. // Pick up special APIs for getting port information.
  41.  
  42. #include <OpenTptConfig.h>
  43.  
  44. /////////////////////////////////////////////////////////////////////
  45. // Pick up the OTDebugBreak and OTAssert macros.
  46.  
  47. #include <OTDebug.h>
  48.  
  49. /////////////////////////////////////////////////////////////////////
  50. // Standard C prototypes.
  51.  
  52. #include <stdio.h>
  53.  
  54. /////////////////////////////////////////////////////////////////////
  55. // OTDebugStr is not defined in any OT header files, but it is
  56. // exported by the libraries, so we define the prototype here.
  57.  
  58. extern pascal void OTDebugStr(const char* str);
  59.  
  60. /////////////////////////////////////////////////////////////////////
  61.  
  62. static OSStatus PrintSerialPortInfo(const OTPortRecord *portRecord)
  63.     // Prints information about the port with the given portRecord.
  64. {
  65.     Str255 userVisibleName;
  66.     
  67.     // OTGetUserPortNameFromPortRef is a little known routine
  68.     // from <OpenTptConfig.h> that allows you to get a user
  69.     // visible name for an Open Transport port.  You must
  70.     // be running PPC code if you want to call this on a PPC machine.
  71.     
  72.     OTGetUserPortNameFromPortRef(portRecord->fRef, userVisibleName);
  73.     
  74.     printf("Found a serial port with port reference $%08lx:\n", portRecord->fRef);
  75.     printf("  User visible name is                       “%#s”.\n", userVisibleName);
  76.     printf("  String to pass to OTCreateConfiguration is “%s”.\n",  portRecord->fPortName);
  77.     printf("  Name of provider module is                 “%s”.\n",  portRecord->fModuleName);
  78.     printf("\n");
  79.     
  80.     return kOTNoError;
  81. }
  82.  
  83. static OSStatus OTFindSerialPorts(void)
  84.     // Lists all of the serial ports on the machine using Open Transport.
  85. {
  86.     OSStatus err;
  87.     Boolean portValid;
  88.     SInt32 portIndex;
  89.     OTPortRecord portRecord;
  90.     UInt16 deviceType;
  91.  
  92.     // Start portIndex at 0 and call OTGetIndexedPort until
  93.     // we find there are no more ports.
  94.         
  95.     portIndex = 0;
  96.     err = kOTNoError;
  97.     do {
  98.         portValid = OTGetIndexedPort(&portRecord, portIndex);
  99.         if (portValid) {
  100.         
  101.             // For each valid port, get the deviceType and, if
  102.             // it's a serial port and not an alias, call PrintSerialPort
  103.             // to dump out its information.  Note that you don't want
  104.             // to include aliases to the serial ports in the list, otherwise
  105.             // a standard machine will have 3 serial ports, "serialA", "serialB"
  106.             // and "serial".
  107.         
  108.             deviceType = OTGetDeviceTypeFromPortRef(portRecord.fRef);
  109.             if (deviceType == kOTSerialDevice && 
  110.                         (portRecord.fInfoFlags & kOTPortIsAlias) == 0) {
  111.                 err = PrintSerialPortInfo(&portRecord);
  112.             }
  113.         }
  114.         portIndex += 1;
  115.     } while ( portValid && err == kOTNoError);
  116.  
  117.     return err;
  118. }
  119.  
  120. /////////////////////////////////////////////////////////////////////
  121.  
  122. void main(void)
  123. {
  124.     OSStatus err;
  125.     
  126.     printf("OTFindSerialPorts\n");
  127.     printf("-- Lists all the serial ports on the machine using OT\n");
  128.     printf("\n");
  129.     
  130.     err = InitOpenTransport();
  131.     
  132.     if (err == noErr) {
  133.     
  134.         err = OTFindSerialPorts();
  135.         
  136.         CloseOpenTransport();
  137.     }
  138.     
  139.     if (err == noErr) {
  140.         printf("Success.\n");
  141.     } else {
  142.         printf("Failed with error %d.\n", err);
  143.     }
  144.     printf("Done.  Press command-Q to Quit.\n");
  145. }
  146.